package com.platform.web.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.data.domain.TempData;
import com.platform.data.service.ITempDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 温度采集曲线图控制类
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/temp")
public class TempReportController extends BaseController {

    private String prefix = "report/temp";

    @Autowired
    private ITempDataService tempDataService;

    @RequiresPermissions("report:temp:view")
    @GetMapping(value = { "", "/{tempId}" })
    public String temp(@PathVariable(value = "tempId", required = false) String tempId, ModelMap mmap)
    {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());
        ca.add(Calendar.MONTH, -1);
        Date resultDate = ca.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mmap.put("beginTime", dateFormat.format(resultDate));
        mmap.put("endTime", dateFormat.format(new Date()));
        mmap.put("tempId",tempId);
        return prefix + "/temp";
    }

    /**
     * 室温变化曲线图
     */
    @PostMapping("/tempSummary")
    @ResponseBody
    public AjaxResult tempSummary(TempData tempData)
    {
        if(tempData.getParams().get("beginTime").equals("") && tempData.getParams().get("endTime").equals("")){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            tempData.getParams().put("beginTime", simpleDateFormat.format(new Date()));
            tempData.getParams().put("endTime", simpleDateFormat.format(new Date()));
        }
        if(tempData.getTempId() != null && !tempData.getTempId().equals("")){
            tempData.setLocationId(null);
        }
        List<TempData> tempDataList  = tempDataService.selectTempDatarList(tempData);
        return AjaxResult.success(tempDataList);
    }

}
